package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    @PostMapping("/list")
    public ResponseResult list(PageRequest pageRequest, @RequestBody CourseDto courseDto){
        return courseBaseService.courseList(pageRequest,courseDto);
    }

    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile uploadFile) throws Exception {
        return courseBaseService.upload(uploadFile.getOriginalFilename(),uploadFile.getInputStream());
    }

    @PostMapping
    public ResponseResult course(@RequestBody CourseBaseDto courseBaseDto){
        return courseBaseService.course(courseBaseDto);
    }

    @GetMapping("/{courseBaseId}")
    public ResponseResult course(@PathVariable("courseBaseId")Long courseId){
        CourseBase courseBase = courseBaseService.getById(courseId);
        CourseBaseVo vo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        return ResponseResult.okResult(vo);
    }

    @PutMapping
    public ResponseResult updateCourse(@RequestBody CourseBaseDto courseBaseDto){
        return courseBaseService.updateCourse(courseBaseDto);
    }

    @DeleteMapping("/{courseId}")
    public ResponseResult deleteCourse(@PathVariable("courseId")Long courseId){
        courseBaseService.removeById(courseId);
        return ResponseResult.okResult();
    }
    // /course/commit/{courseBaseId}
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId){
        return courseBaseService.commit(courseBaseId);
    }

    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto){
        return courseBaseService.approve(dto);
    }
}
